package com.j180.excepetion;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@Component
public class MyExceptionHandler implements HandlerExceptionResolver {

    /**
     *
     * @param httpServletRequest 请求
     * @param httpServletResponse 响应
     * @param target  目标对象
     * @param e  发生的异常
     * @return
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object target, Exception e) {

        System.out.println("目标"+target);

        ModelAndView mv = new ModelAndView();

         MyException myException = (MyException) e;

         mv.addObject("msg",myException.showError());
         mv.setViewName("error");

        return mv;
    }
}
